package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.model.Rebate;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.ProductService;
import com.yami.shop.service.RebateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 *
 */
@RestController
@RequestMapping("/admin/rebate")
public class RebateController {
    @Autowired
    private  RebateService rebateService;

    @Autowired
    private ProductService productService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<Rebate>> page(Rebate rebate, PageParam<Rebate> page) {
        IPage<Rebate> userIPage = rebateService.page(page, new LambdaQueryWrapper<Rebate>().eq(Rebate::getShopId, SecurityUtils.getSysUser().getShopId()));
        return ResponseEntity.ok(userIPage);
    }

    /**
     * 修改
     */
    @PutMapping("/updateRebate")
    public ResponseEntity<Boolean> updateRebate(Rebate rebate){
        boolean flag = rebateService.updateById(rebate);
        //获取当前返利状态
        String status = rebate.getStatus();
        if(rebate!=null) {
            Long flid = rebate.getFlid();
            Rebate rebate1 = rebateService.getById(flid);
            Long prodId = rebate1.getProdId();
            Product product = productService.getProductByProdId(prodId);
            if(status.equals("1")){
                product.setIsSpread(1);
            }else if(status.equals("0")){
                product.setIsSpread(0);
            }
            productService.saveOrUpdate(product);
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 新增返利数据
     */
    @PutMapping("/insertRebate")
    public ResponseEntity<Boolean> insertRebate(@Valid Rebate rebate){
        rebate.setCreateTime(new Date());
        Long shopId =SecurityUtils.getSysUser().getShopId();
        rebate.setShopId(shopId);
        boolean flag = rebateService.saveData(rebate);
        return ResponseEntity.ok(flag);
    }

    /**
     * 删除返利数据
     */
    @PutMapping("/deleteRebate")
    public ResponseEntity<Boolean> deleteRebate( Long flid){
        boolean flag = false;
        if(flid!=null) {
            Rebate rebate1 = rebateService.getById(flid);
            Long prodId = rebate1.getProdId();
            Product product = productService.getProductByProdId(prodId);
            if(product!=null) {
                product.setIsSpread(1);
                productService.saveOrUpdate(product);
            }
            flag = rebateService.removeById(flid);
        }
        return ResponseEntity.ok(flag);
    }

}
